<?php

namespace app\common\business;

use app\common\model\mysql\SOrder as SOrderModel;
use app\common\model\mysql\SOrderRefund as SOrderRefundModel;

class SOrder
{
    public $orderObj = null;
    public $orderRefund = null;
    public function __construct()
    {
        $this->orderObj = new SOrderModel();
        $this->orderRefund = new SOrderRefundModel();
    }

    /**
     * 查询订单列表
     * @param $where
     * @param $field
     * @param $limit
     * @param $page
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getNormalOrderData($where,$field,$limit,$page){
        $dictData = $this->orderObj->getNormalOrderList($where,$field,$limit,$page);
        if(!$dictData){
            return [];
        }
        $dictData = $dictData->toArray();
        return $dictData;
    }

    //添加订单
    public function addOrder($data){
        try {
            $this->orderObj->save($data);
        }catch (\Exception $e){
            throw new \think\Exception($e->getMessage());
        }
        return $this->orderObj->id;
    }

    /**
     * 根据主键id更新数据表中的数据
     * @param $id
     * @param $data
     * @return bool
     */
    public function updateById($id,$data){
        $id = intval($id);
        if(empty($id) || empty($data) || !is_array($data)){
            return false;
        }
        $where = [
            "order_id"=>$id,
        ];
        return $this->orderObj->where($where)->save($data);
    }

    public function checkOrderVerifyCode($verifyCode){
        if(empty($verifyCode)){
            return false;
        }
        $where = [
            "verify_code"=>$verifyCode,
            "paid"=>1,
            "order_type"=>1,
            "status"=>1,
            "verify_status"=>0
        ];
        $result = $this->orderObj->where($where)->find();
        return $result ? $result->toArray() : [];
    }

    public function sellOrder($verifyCode,$orderId,$uid){
        $orderId = intval($orderId);
        if(empty($orderId)){
            return false;
        }
        $where = [
            "order_id"=>$orderId,
            "verify_code"=>$verifyCode,
        ];
        $data = [
            "status"=>3,
            "verify_status"=>2,
            "verify_time"=>date("Y-m-d H:i:s",time()),
            "verify_service_id"=>$uid,
        ];
        return $this->orderObj->where($where)->save($data);
    }

    //根据$id,去数据库查询是否存在这条记录
    public function getOrderById($id,$field = '*'){
        $user = $this->orderObj->getOrderByIdFind($id,$field);
        if(!$user){
            return [];
        }
        return $user->toArray();
    }

    //根据$ids数组，删除数据库记录delete
    public function delOrderIds($ids){
        $ids = explode(',', $ids);
        $postNameResult = $this->orderObj->getOrderByDelFlagId($ids);
        if($postNameResult){
            throw new \think\Exception("您选择的的订单存在用户未删除的订单，无法删除用户未删除的订单！");
        }
        $result = $this->orderObj->DelOrderIds($ids);
        return $result;
    }

    public function payOrderIds($ids){
        $ids = explode(',', $ids);
        $postNameResult = $this->orderObj->getOrderByPayTypeId($ids);
        if($postNameResult){
            throw new \think\Exception("您选择的的订单状态异常，不允许立即付款！");
        }
        $result = $this->orderObj->PayOrderIds($ids);
        return $result;
    }

    public function refundOrderIds($ids){
        $ids = explode(',', $ids);
        $result = $this->orderObj->refundOrderIds($ids);
        return $result;
    }

    public function receiveOrderIds($ids){
        $ids = explode(',', $ids);
        $result = $this->orderObj->receiveOrderIds($ids);
        return $result;
    }

    /******************* 售后订单管理模块 ********************************/
    /**
     * 查询售后订单列表
     * @param $where
     * @param $field
     * @param $limit
     * @param $page
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getNormalOrderRefundData($where,$field,$limit,$page){
        $dictData = $this->orderRefund->getNormalOrderRefundList($where,$field,$limit,$page);
        if(!$dictData){
            return [];
        }
        $dictData = $dictData->toArray();
        return $dictData;
    }

    /**
     * 根据主键id更新数据表中的数据
     * @param $id
     * @param $data
     * @return bool
     */
    public function updateRefundById($id,$data){
        $id = intval($id);
        if(empty($id) || empty($data) || !is_array($data)){
            return false;
        }
        $where = [
            "order_refund_id"=>$id,
        ];
        return $this->orderRefund->where($where)->save($data);
    }

    //添加售后订单
    public function addRefundOrder($data){
        try {
            $this->orderRefund->save($data);
        }catch (\Exception $e){
            throw new \think\Exception($e->getMessage());
        }
        return $this->orderRefund->id;
    }

    //根据$orderId,去数据库查询是否存在这条记录
    public function getRefundOrderById($orderId,$field = '*'){
        $user = $this->orderRefund->getRefundOrderByIdFind($orderId,$field);
        if(!$user){
            return [];
        }
        return $user->toArray();
    }
}
